package controllers

import (
	"read.com/admin/models"
	"encoding/base64"
)

type MenuController struct {
	BaseController
}

func (self *MenuController) List(){
	self.Data["pageTitle"] = "菜单管理"
	appList := models.GetLinkageApp()
	self.Data["app"] = appList
	self.display()
}

func (self *MenuController) Table(){
	appId,_ := self.GetInt("appId")
	//列表参数
	page, err := self.GetInt("page")
	if err != nil {
		page = 1
	}
	limit, err := self.GetInt("limit")
	if err != nil {
		limit = 10
	}
	result,count := models.GetMenuList(appId,page,limit)
	self.ajaxList("成功", MSG_OK, count, result)
}

func (self *MenuController) AddFirst(){
	appId,_ := self.GetInt("appId")
	isOuter,_ := self.GetInt8("isOuter")
	base64.StdEncoding.EncodeToString([]byte(self.GetString("menuName")))
	menuName := base64.StdEncoding.EncodeToString([]byte(self.GetString("menuName")))
	menuPath := self.GetString("menuPath")
	sortId,_ := self.GetInt("sortId")
	err := models.AddMenu(appId,0,isOuter,menuName,menuPath,sortId)
	if err != nil {
		self.ajaxMsg("新增失败",MSG_ERR)
	}else {
		self.ajaxMsg("新增成功",MSG_OK)
	}
}


func (self *MenuController) EditFirst(){
	id,_ := self.GetInt("id")
	isOuter,_ := self.GetInt8("isOuter")
	menuName := self.GetString("menuName")
	menuPath := self.GetString("menuPath")
	sortId,_ := self.GetInt("sortId")
	err := models.EditMenu(id,isOuter,menuName,menuPath,sortId)
	if err != nil {
		self.ajaxMsg("编辑失败",MSG_ERR)
	}else {
		self.ajaxMsg("编辑成功",MSG_OK)
	}
}


func (self *MenuController) DelFirst(){
	id,_ := self.GetInt("id")
	err := models.DelFirstLevelMenu(id)
	if err != nil {
		self.ajaxMsg("删除失败",MSG_ERR)
	}else {
		self.ajaxMsg("删除成功",MSG_OK)
	}
}

func (self *MenuController) Second(){
	id,_ := self.GetInt("id")
	appId,_ := self.GetInt("appId")
	name := self.GetString("name")
	appName := self.GetString("appName")
	self.Data["name"] = name
	self.Data["appId"] = appId
	self.Data["appName"] = appName
	self.Data["pageTitle"] = "二级菜单管理"
	self.Data["id"] = id
	self.display()
}

func (self *MenuController) Secondtable(){
	id,_ := self.GetInt("id")
	name := self.GetString("name")
	//列表参数
	page, err := self.GetInt("page")
	if err != nil {
		page = 1
	}
	limit, err := self.GetInt("limit")
	if err != nil {
		limit = 10
	}
	result,count := models.GetSecondMenu(id,name,page,limit)
	self.ajaxList("成功", MSG_OK, count, result)
}

func (self *MenuController) AddSecond(){
	appId,_ := self.GetInt("appId")
	mainId,_ := self.GetInt("mainId")
	isOuter,_ := self.GetInt8("isOuter")
	menuName := base64.StdEncoding.EncodeToString([]byte(self.GetString("menuName")))
	menuPath := self.GetString("menuPath")
	sortId,_ := self.GetInt("sortId")
	err := models.AddMenu(appId,mainId,isOuter,menuName,menuPath,sortId)
	if err != nil {
		self.ajaxMsg("新增失败",MSG_ERR)
	}else {
		self.ajaxMsg("新增成功",MSG_OK)
	}
}

func (self *MenuController) EditSecond(){
	id,_ := self.GetInt("id")
	isOuter,_ := self.GetInt8("isOuter")
	menuName := self.GetString("menuName")
	menuPath := self.GetString("menuPath")
	sortId,_ := self.GetInt("sortId")
	err := models.EditMenu(id,isOuter,menuName,menuPath,sortId)
	if err != nil {
		self.ajaxMsg("编辑失败",MSG_ERR)
	}else {
		self.ajaxMsg("编辑成功",MSG_OK)
	}
}

func (self *MenuController) DelSecond(){
	id,_ := self.GetInt("id")
	err := models.DelSecondLevelMenu(id)
	if err != nil {
		self.ajaxMsg("删除失败",MSG_ERR)
	}else {
		self.ajaxMsg("删除成功",MSG_OK)
	}
}